﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMS.Utilities;
using PMS.ENT;
using PMS.BLL;


namespace PMS.Controllers
{
    public class SettingController : Controller
    {
       
        #region Account Info
        public ActionResult AccountInfo()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                #endregion

                UserENT user = UserBLL.GetUserByID(Utility.UserID);
                ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), user.DepartmentID);
                return View(user);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AccountInfo(string fullname, DateTime dateOfBirth, string sex, string email, string DepartmentID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                #endregion

                UserENT user = UserBLL.GetUserByID(Utility.UserID);
                user.FullName = fullname;
                user.DateOfBirth = dateOfBirth;
                user.Sex = sex;
                user.Email = email;

                //Update DepartmentID
                user.DepartmentID = Utility.IntParse(DepartmentID);

                if (UserBLL.UpdateUser(user) > 0)
                {
                    TempData["Message"] = "Success, Update User Info!";
                }
                else
                {
                    TempData["Message"] = "Error, Update User Info!";
                }
                return RedirectToAction("AccountInfo", "Setting");
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Change Password
        public ActionResult ChangePassword()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                #endregion
                return View();
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ChangePassword(string oldPassword, string newPassword, string confirmPassword)
        {

            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                #endregion

                bool isValidate = true;
                #region Validate
                if (string.IsNullOrWhiteSpace(oldPassword))
                {
                    TempData["Message"] = "Error, Enter Old Password!";
                    isValidate = false;
                }
                if (string.IsNullOrWhiteSpace(newPassword))
                {
                    TempData["Message"] = "Error, Enter New Password!";
                    isValidate = false;
                }
                if (string.IsNullOrWhiteSpace(confirmPassword))
                {
                    TempData["Message"] = "Error, Enter Confirm Password!";
                    isValidate = false;
                }
                if (newPassword != confirmPassword)
                {
                    TempData["Message"] = "Error, New Password Does Not Match!";
                    isValidate = false;
                }
                #endregion

                if (isValidate)
                {
                    UserENT user = UserBLL.GetUserByID(Utility.UserID);

                    if (user.Password != oldPassword)
                    {
                        TempData["Message"] = "Error, Invalid Password!";
                    }
                    else
                    {
                        user.Password = newPassword;

                        if (UserBLL.UpdateUser(user) > 0)
                        {
                            TempData["Message"] = "Success, Update Password!";
                        }
                        else
                        {
                            TempData["Message"] = "Error, Update Password!";
                        }
                    }
                }

                return View();
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region General Setting
        public ActionResult GeneralSetting()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GeneralSetting(int id)
        {
            return View();
        }
        #endregion

        #region Role
        public ActionResult Role()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Role(int id)
        {
            return View();
        }
        #endregion

        #region User Group
        public ActionResult UserGroup()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UserGroup(int id)
        {
            return View();
        }
        #endregion

        #region Manage User

        #region List
        public ActionResult ManageUser()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;

                UserCollection listUser = UserBLL.GetUserList(string.Empty, 0, 0, UserColumns.FullName.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                LoadData(0, 0);
                #endregion

                #region Pagination and search
                ViewData["UserName"] = string.Empty;
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = UserColumns.UserName.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listUser);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ManageUser(int[] userID, string action, int isSearch, string fullName, int departmentID, int roleID, string orderBy, string orderDirection, int page, int pageSize)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {

                    if (UserBLL.DeleteUser(Utility.StringParse(userID), Utility.UserID) > 0)
                    {
                        TempData["Message"] = "Success, delete!";
                    }

                }
                #endregion

                #region LoadData
                int totalRecords = 0;

                UserCollection listUser = UserBLL.GetUserList(fullName, departmentID, roleID, orderBy, orderDirection, page, pageSize, out totalRecords);

                LoadData(departmentID, roleID);
                #endregion

                #region Pagination
                ViewData["UserName"] = fullName;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listUser);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #endregion

        #region Add User
        public ActionResult AddUser()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                UserENT user = new UserENT
                {
                    DateOfBirth = DateTime.Today,
                };

                LoadData(user.DepartmentID, user.RoleID);

                ViewData["Action"] = ActionName.Insert.ToString();
                #endregion

                return View("User", user);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AddUser(UserENT user)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                user.CreatedBy = Utility.UserID;
                user.UpdateBy = Utility.UserID;

                int result = UserBLL.InsertUser(user);
                if (result > 0)
                {
                    TempData["Message"] = "Success, insert user!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, User name exists!";

                }

                #endregion

                #region LoadData

                LoadData(user.DepartmentID, user.RoleID);


                #endregion

                return RedirectToAction("EditUser", new { id = result });
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Edit User
        public ActionResult EditUser(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] userID = { ID };



                UserENT user = UserBLL.GetUserByID(ID);

                LoadData(user.DepartmentID, user.RoleID);

                ViewData["Action"] = ActionName.Update.ToString();
                #endregion

                return View("User", user);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult EditUser(int ID, UserENT user)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                user.UpdateBy = Utility.UserID;

                int result = UserBLL.UpdateUser(user);
                if (result > 0)
                {
                    TempData["Message"] = "Success, Update user!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, User name exists!";
                }

                #endregion

                #region LoadData

                LoadData(user.DepartmentID, user.RoleID);

                ViewData["Action"] = ActionName.Update.ToString();

                #endregion

                return View("User", user);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region View User
        public ActionResult ViewUser(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] userID = { ID };


                UserENT user = UserBLL.GetUserByID(ID);

                LoadData(user.DepartmentID, user.RoleID);

                ViewData["Action"] = ActionName.View.ToString();
                #endregion

                return View("User", user);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region LoadData
        public void LoadData(int departmentID, int roleID)
        {
            ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), departmentID);
            ViewData["RoleList"] = new SelectList(RoleBLL.GetRoleListForCMB(), RoleColumns.RoleID.ToString(), RoleColumns.RoleName.ToString(), roleID);

        }

        #endregion
        #endregion

        #region Manage Semester

        #region List
        public ActionResult ManageSemester()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;

                SemesterCollection listSemester = SemesterBLL.GetSemesterList(string.Empty, SemesterColumns.CreatedDate.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                #endregion

                #region Pagination and search
                ViewData["SemesterName"] = string.Empty;
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = SemesterColumns.CreatedDate.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listSemester);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ManageSemester(int[] semesterID, string action, int isSearch, string SemesterName, string orderBy, string orderDirection, int page, int pageSize)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {

                    if (SemesterBLL.DeleteSemester(Convert.ToInt32(Utility.StringParse(semesterID)), Utility.UserID) > 0)
                    {
                        TempData["Message"] = "Success, delete!";
                    }

                }
                #endregion

                #region LoadData
                int totalRecords = 0;

                SemesterCollection listSemester = SemesterBLL.GetSemesterList(SemesterName, orderBy, orderDirection, page, pageSize, out totalRecords);


                #endregion

                #region Pagination
                ViewData["SemesterName"] = SemesterName;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listSemester);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #endregion

        #region Add Semester
        public ActionResult AddSemester()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                SemesterENT semester = new SemesterENT
                {
                };


                ViewData["Action"] = ActionName.Insert.ToString();
                #endregion

                return View("Semester", semester);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AddSemester(SemesterENT semester)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                semester.CreatedBy = Utility.UserID;
                semester.UpdateBy = Utility.UserID;

                int result = SemesterBLL.InsertSemester(semester);
                if (result > 0)
                {
                    TempData["Message"] = "Success, insert semester!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error !";

                }

                #endregion


                return RedirectToAction("EditSemester", new { id = result });
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Edit Semester
        public ActionResult EditSemester(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] semesterID = { ID };



                SemesterENT semester = SemesterBLL.GetSemesterByID(ID);


                ViewData["Action"] = ActionName.Update.ToString();
                #endregion

                return View("Semester", semester);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult EditSemester(int ID, SemesterENT semester)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                semester.UpdateBy = Utility.UserID;

                int result = SemesterBLL.UpdateSemester(semester);
                if (result > 0)
                {
                    TempData["Message"] = "Success, Update semester!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error!";
                }

                #endregion

                #region LoadData


                ViewData["Action"] = ActionName.Update.ToString();

                #endregion

                return View("Semester", semester);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region View Semester
        public ActionResult ViewSemester(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] semesterID = { ID };


                SemesterENT semester = SemesterBLL.GetSemesterByID(ID);


                ViewData["Action"] = ActionName.View.ToString();
                #endregion

                return View("Semester", semester);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion



        #endregion

        #region Manage Department

        #region List
        public ActionResult ManageDepartment()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;

                DepartmentCollection listDepartment = DepartmentBLL.GetDepartmentList(string.Empty, DepartmentColumns.CreatedDate.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                #endregion

                #region Pagination and search
                ViewData["DepartmentName"] = string.Empty;
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = DepartmentColumns.CreatedDate.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listDepartment);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ManageDepartment(int[] DepartmentID, string action, int isSearch, string DepartmentName, string orderBy, string orderDirection, int page, int pageSize)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {

                    if (DepartmentBLL.DeleteDepartment(Convert.ToInt32(Utility.StringParse(DepartmentID)), Utility.UserID) > 0)
                    {
                        TempData["Message"] = "Success, delete!";
                    }

                }
                #endregion

                #region LoadData
                int totalRecords = 0;

                DepartmentCollection listDepartment = DepartmentBLL.GetDepartmentList(DepartmentName, orderBy, orderDirection, page, pageSize, out totalRecords);


                #endregion

                #region Pagination
                ViewData["DepartmentName"] = DepartmentName;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listDepartment);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #endregion

        #region Add Department
        public ActionResult AddDepartment()
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                DepartmentENT Department = new DepartmentENT
                {
                };


                ViewData["Action"] = ActionName.Insert.ToString();
                #endregion

                return View("Department", Department);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AddDepartment(DepartmentENT Department)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                Department.CreatedBy = Utility.UserID;
                Department.UpdateBy = Utility.UserID;

                int result = DepartmentBLL.InsertDepartment(Department);
                if (result > 0)
                {
                    TempData["Message"] = "Success, insert Department!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error !";

                }

                #endregion


                return RedirectToAction("EditDepartment", new { id = result });
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Edit Department
        public ActionResult EditDepartment(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] DepartmentID = { ID };



                DepartmentENT Department = DepartmentBLL.GetDepartmentByID(ID);


                ViewData["Action"] = ActionName.Update.ToString();
                #endregion

                return View("Department", Department);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult EditDepartment(int ID, DepartmentENT Department)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                Department.UpdateBy = Utility.UserID;

                int result = DepartmentBLL.UpdateDepartment(Department);
                if (result > 0)
                {
                    TempData["Message"] = "Success, Update Department!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error!";
                }

                #endregion

                #region LoadData


                ViewData["Action"] = ActionName.Update.ToString();

                #endregion

                return View("Department", Department);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region View Department
        public ActionResult ViewDepartment(int ID)
        {
            try
            {
                #region Permission
                //if (Session["UserID"] == null || Utility.IntParse(Session["UserID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.UserID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] DepartmentID = { ID };


                DepartmentENT Department = DepartmentBLL.GetDepartmentByID(ID);


                ViewData["Action"] = ActionName.View.ToString();
                #endregion

                return View("Department", Department);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion



        #endregion

        #region Manage Subject

        #region List
        public ActionResult ManageSubject()
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData
                int page = MvcApplication.pageDefault;
                int pageSize = MvcApplication.pageSizeDefault;
                int totalRecords = 0;

                SubjectCollection listSubject = SubjectBLL.GetSubjectList(string.Empty, string.Empty, 0, SubjectColumns.SubjectName.ToString(), OrderDirection.ASC.ToString(), page, pageSize, out totalRecords);

                LoadData(0);
                #endregion

                #region Pagination and search
                ViewData["SubjectName"] = string.Empty;
                ViewData["IsSearch"] = 0;
                ViewData["OrderBy"] = SubjectColumns.SubjectName.ToString();
                ViewData["OrderDirection"] = OrderDirection.ASC.ToString();
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listSubject);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult ManageSubject(int[] SubjectID, string action, int isSearch, string subjectname, string subjectcode, int departmentID, string orderBy, string orderDirection, int page, int pageSize)
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                if (action == ActionName.Delete.ToString())
                {

                    if (SubjectBLL.DeleteSubject(Convert.ToInt32(Utility.StringParse(SubjectID)), Utility.UserID) > 0)
                    {
                        TempData["Message"] = "Success, delete!";
                    }

                }
                #endregion

                #region LoadData
                int totalRecords = 0;

                SubjectCollection listSubject = SubjectBLL.GetSubjectList(subjectname,subjectcode, departmentID, orderBy, orderDirection, page, pageSize, out totalRecords);

                LoadData(departmentID);
                #endregion

                #region Pagination
                ViewData["SubjectName"] = subjectname;
                ViewData["IsSearch"] = isSearch;
                ViewData["OrderBy"] = orderBy;
                ViewData["OrderDirection"] = orderDirection;
                ViewData["Page"] = page;
                ViewData["PageSize"] = pageSize;
                double PageNum = Math.Ceiling((double)totalRecords / pageSize);
                ViewData["PageNum"] = PageNum;
                ViewData["TotalRecord"] = totalRecords;

                if (totalRecords > 0)
                {
                    if (page == PageNum)
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), totalRecords.ToString(), totalRecords.ToString());
                    else
                        ViewData["PageInfo"] = string.Format(" {0} - {1} / {2}.", (((page - 1) * pageSize) + 1).ToString(), (pageSize * page).ToString(), totalRecords.ToString());
                }
                #endregion

                return View(listSubject);
            }
            catch (DataAccessException ex)
            {
                TempData[PageInfo.Message.ToString()] = ex.Message;
                return RedirectToAction("Error", "Home"); ;
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        #endregion

        #region Add Subject
        public ActionResult AddSubject()
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                SubjectENT Subject = new SubjectENT
                {
                };

                LoadData(Subject.DepartmentID);

                ViewData["Action"] = ActionName.Insert.ToString();
                #endregion

                return View("Subject", Subject);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult AddSubject(SubjectENT Subject)
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                Subject.CreatedBy = Utility.UserID;
                Subject.UpdateBy = Utility.UserID;

                int result = SubjectBLL.InsertSubject(Subject);
                if (result > 0)
                {
                    TempData["Message"] = "Success, insert Subject!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error!";

                }

                #endregion

                #region LoadData

                LoadData(Subject.DepartmentID);


                #endregion

                return RedirectToAction("EditSubject", new { id = result });
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region Edit Subject
        public ActionResult EditSubject(int ID)
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] SubjectID = { ID };



                SubjectENT Subject = SubjectBLL.GetSubjectByID(ID);

                LoadData(Subject.DepartmentID);

                ViewData["Action"] = ActionName.Update.ToString();
                #endregion

                return View("Subject", Subject);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }

        [HttpPost]
        public ActionResult EditSubject(int ID, SubjectENT Subject)
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}
                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region Do Action
                Subject.UpdateBy = Utility.UserID;

                int result = SubjectBLL.UpdateSubject(Subject);
                if (result > 0)
                {
                    TempData["Message"] = "Success, Update Subject!";
                }
                else if (result == -1)
                {
                    TempData["Message"] = "Error, Subject name exists!";
                }

                #endregion

                #region LoadData

                LoadData(Subject.DepartmentID);

                ViewData["Action"] = ActionName.Update.ToString();

                #endregion

                return View("Subject", Subject);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region View Subject
        public ActionResult ViewSubject(int ID)
        {
            try
            {
                #region Permission
                //if (Session["SubjectID"] == null || Utility.IntParse(Session["SubjectID"]) <= 0)
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                //if (Utility.SubjectID <= 0 || Utility.RoleID <= 0 || string.IsNullOrWhiteSpace(Utility.Role))
                //{
                //    return RedirectToAction("Login", "Home");
                //}

                if (Utility.RoleID != 1)
                {
                    return RedirectToAction("Dashboard", "Home");
                }
                #endregion

                #region LoadData

                int[] SubjectID = { ID };


                SubjectENT Subject = SubjectBLL.GetSubjectByID(ID);

                LoadData(Subject.DepartmentID);

                ViewData["Action"] = ActionName.View.ToString();
                #endregion

                return View("Subject", Subject);
            }
            catch (DataAccessException)
            {
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
            catch (Exception ex)
            {
                LogBLL.LogEx("BLM_ERR_Common", ex);
                TempData[PageInfo.Message.ToString()] = "BLM_ERR_Common";
                return RedirectToAction("Error", "Home");
            }
        }
        #endregion

        #region LoadData
        public void LoadData(int departmentID)
        {
            ViewData["DepartmentList"] = new SelectList(DepartmentBLL.GetDepartmentListForCMB(), DepartmentColumns.DepartmentID.ToString(), DepartmentColumns.DepartmentName.ToString(), departmentID);

        }

        #endregion
        #endregion
    }
}
